package com.honeybee.cloud.framework.oauth.handler;

import com.honeybee.cloud.framework.base.exception.ExceptionEnum;
import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.handler.CommonExceptionHandler;
import com.honeybee.cloud.framework.common.response.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;


/**
 * @Description 公共异常handler  api层可以继承此类，加上注解@ControllerAdvice 来达到全局处理异常的情况
 * @Author cheng
 * @Create 2018-12-08
 * @Since 1.0.0
 */
@Slf4j
public abstract class OauthCommonExceptionHandler extends CommonExceptionHandler {


    /**
     * AccessDeniedException  统一只展示系统异常给前端
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    public ResponseEntity handleAccessDeniedException(AccessDeniedException ex) {
        log.error("AccessDeniedException", ex);
        return ResponseEntity.error(BaseBizException.getInstance(ExceptionEnum.AUTHORIZE));
    }

}
